/*
 * hudong.com Inc.
 * Copyright (c) 2000-2004 All Rights Reserved.
 */
package middlegen.plugins.ibatis;

import middlegen.javax.Sql2Java;
import middlegen.plugins.ibatis.config.ColumnConfig;
import middlegen.plugins.ibatis.util.Util;

import org.apache.commons.lang.StringUtils;

public class IWalletResultMapResult implements ResultMapResult {
    /** the column associated with the result map */
    protected IWalletColumn column;

    protected ColumnConfig  columnConfig;

    /** 
     * Constructor for IWalletResultMapResult.
     */
    public IWalletResultMapResult(IWalletColumn c) {
        column = c;
        this.columnConfig = c.getColumnConfig();
        if (null != this.columnConfig) {
            System.out.println(columnConfig);
        }
    }

    /**
     * @return
     *
     * @see middlegen.plugins.ibatis.ResultMapResult#getProperty()
     */
    public String getProperty() {
        if (column != null) {
            if (IWalletPlugin.MONEY_CLASS.equals(column.getJavaType())) {
                return column.getVariableName() + ".cent";
            } else {
                return column.getVariableName();
            }
        } else {
            return null;
        }
    }

    /**
     * @return
     *
     * @see middlegen.plugins.ibatis.ResultMapResult#getColumn()
     */
    public String getColumn() {
        if (column != null) {
            return column.getSqlName();
        } else {
            return null;
        }
    }

    /**
     * @return
     *
     * @see middlegen.plugins.ibatis.ResultMapResult#getColumnIndex()
     */
    public int getColumnIndex() {
        // not used yet
        return -1;
    }

    /**
     * @return
     *
     * @see middlegen.plugins.ibatis.ResultMapResult#getJavaType()
     */
    public String getJavaType() {
        if (column != null) {
            if (IWalletPlugin.MONEY_CLASS.equals(column.getJavaType())) {
                // for Money, we map between db column and Money property cent
                return "long";
            } else {
                return column.getJavaType();
            }
        } else {
            return null;
        }
    }

    /**
     * @return
     *
     * @see middlegen.plugins.ibatis.ResultMapResult#getJdbcType()
     */
    public String getJdbcType() {
        if (column != null) {
            String origJdbcType = column.getSqlTypeName();

            if (StringUtils.equalsIgnoreCase(origJdbcType, "DATE")) {
                return "DATETIME";
            } else {
                return origJdbcType;
            }
        } else {
            return null;
        }
    }

    /**
     * @return
     *
     * @see middlegen.plugins.ibatis.ResultMapResult#getNullValue()
     */
    public String getNullValue() {
        //      if (column != null) {
        //          return column.getResultMapNullValue();
        //      } else {
        //          return null;
        //      }
        return Util.getDefaultValue(getJavaType());
    }

    public String getTypeHandler() {
        if (null != this.columnConfig) {
            return this.columnConfig.getTypeHandler();
        }
        return null;
    }

    public boolean hasTypeHandler() {
        if (null == this.columnConfig || StringUtils.isBlank(this.columnConfig.getTypeHandler())) {
            return false;
        }
        return true;
    }

    /**
     * @return
     *
     * @see middlegen.plugins.ibatis.ResultMapResult#isHasNullValue()
     */
    public boolean isHasNullValue() {
        return Sql2Java.isPrimitive(getJavaType());
        //      if (column != null) {
        //          return column.isNeedResultMapNullValue();
        //      } else {
        //          return false;
        //      }
    }

    /**
     * @return
     *
     * @see middlegen.plugins.ibatis.ResultMapResult#getSelect()
     */
    public String getSelect() {
        // not supported yet
        return "";
    }
}
